<?php

namespace App\Web\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function login(Request $request)
    {
        $this->validateLogin($request);

        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        $resault = $this->attemptLogin($request);
        if ($resault == 'success') {
            $user = \Auth::user();
            $user->login_last_time = Carbon::now();
            $user->login_last_ip = $request->ip();
            $user->save();

            myflash()->success('欢迎回来！');
            return $this->sendLoginResponse($request);

        }elseif ($resault == 'lock'){
            return $this->sendLockLoginResponse($request);
        }else{
            $this->incrementLoginAttempts($request);
            return $this->sendFailedLoginResponse($request);
        }
    }

    protected function attemptLogin(Request $request)
    {
        $login_data = $this->credentials($request);
        $login_lock_data = array_merge($login_data, ['status' => 1]);

        $res = $this->guard()->attempt(
            $login_lock_data, $request->has('remember')
        );
        if (!$res){
            $user = User::where('email', $login_data['email'])->first();
            if ($user && \Hash::check(
                    $login_data['password'],
                    $user->password
                )){
                return 'lock'; //账号被禁用
            }else{
                return 'fail'; //账号密码错误
            }
        }

        return 'success'; //登录成功
    }

    /**
     * 账号禁用错误
     * @param Request $request
     * @throws ValidationException
     */
    protected function sendLockLoginResponse(Request $request)
    {
        throw ValidationException::withMessages([
            $this->username() => [trans('auth.lock')],
        ]);
    }

    /**
     * Log the user out of the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function logout(Request $request)
    {
        \Auth::guard('web')->logout();

        $request->session()->invalidate();

        myflash()->success('退出成功！');
        return redirect('/');
    }



}
